﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;



using Meqs.Entity;
using Meqs.Logic.Interface;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;
using log4net;

namespace Meqs.Logic.Implement
{
    public class StuCauseEvalItemLogic : CommonLogic<StuCauseEvalItem, int>, IStuCauseEvalItemLogic
    {
        private ISchoolInfoLogic _schoolLogic;
        public StuCauseEvalItemLogic(ISchoolInfoLogic schoolLogic)
        {
            _schoolLogic = schoolLogic;
        }
        #region Implementation of ICommonLogic<StuCauseEvalItem, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(StuCauseEvalItem entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(StuCauseEvalItem entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public StuCauseEvalItem FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<StuCauseEvalItem> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<StuCauseEvalItem> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        #region Implementation of IStuCauseEvalItemLogic

        /// <summary>
        /// 获取学分项目
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<StuCauseEvalItemView> GetStuCauseEvalItemList(string schoolMark)
        {
            SchoolInfo school = _schoolLogic.FindBySchoolMark(schoolMark);
            int schoolYear = school.SchoolYear;
            int term = school.SchoolTerm;
            return GetStuCauseEvalItemList(schoolMark, schoolYear, term);
        }

        /// <summary>
        /// 获取学分项目
        /// </summary>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<StuCauseEvalItemView> GetStuCauseEvalItemList(string schoolMark, int schoolYear, int term)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("SchoolYear", schoolYear);
            ht.Add("Term", term);
            return FindCustomer<StuCauseEvalItemView>("StuCauseEvalItem.GetStuCauseEvalItemList", ht);
        }

        /// <summary>
        /// 获取学分小类
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="typeID"></param>
        /// <returns></returns>
        public IList<StuCauseEvalItem> GetItemByType(string schoolMark, int typeID)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("TypeID", typeID);
            return FindCustomer<StuCauseEvalItem>("StuCauseEvalItem.Find", ht);
        }

        /// <summary>
        /// 获取学分单项
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="itemID"></param>
        /// <returns></returns>
        public StuCauseEvalItem GetItemByID(string schoolMark, int itemID)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("RID", itemID);
            return FindCustomerByID<StuCauseEvalItem>("StuCauseEvalItem.FindById", ht);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        public int DeleteById(string schoolMark,int rid)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("RID", rid);

            return DeleteCustomer("StuCauseEvalItem.DeleteById", table);
        }

        /// <summary>
        /// 使用事务，先删除，再添加
        /// </summary>
        /// <param name="listDel"></param>
        /// <param name="listAdd"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int InsertCauseItem(string schoolMark,List<VStuCauseEvalHistoryTypeItem> listDel, List<VStuCauseEvalHistoryTypeItem> listAdd)
        {
            int result = 0;
            try
            {
                foreach (VStuCauseEvalHistoryTypeItem delItem in listDel)
                {
                    //Delete(delItem.ItemID);
                    DeleteById(schoolMark, delItem.ItemID);
                }
                foreach (VStuCauseEvalHistoryTypeItem addItem in listAdd)
                {
                    Hashtable table = new Hashtable();
                    table.Add("SchoolMark", schoolMark);
                    table.Add("typeId", addItem.TypeID);
                    table.Add("historyId", addItem.HistoryID);
                    table.Add("causeId", addItem.CauseID);
                    table.Add("itemCaption", addItem.ItemCaption);
                    table.Add("itemType", addItem.ItemType);
                    table.Add("operator", addItem.Operator);
                    table.Add("param", addItem.Param);

                    int id = (int)InsertCustomer("StuCauseEvalItem.InsertItem", table);
                }
                result = 1;
            }
            catch (Exception e)
            {
                //throw;
                result = 0;
            }
            return result;
        }


        /// <summary>
        /// 删除大类下的项
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public int DeleteByTypeId(string schoolMark, int typeId)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("typeId", typeId);

            return DeleteCustomer("StuCauseEvalItem.DeleteByTypeId", table);
        }

        #endregion
    }
}